<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\FourRefund;
use App\Models\Fourbusiness;
use App\Models\FourMethod;
use App\Models\FourPatient;
use App\Models\FourSource;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class FourRefundController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FourRefund(['es','source','type','pay','des','met']), function (Grid $grid) {
            //$grid->column('id')->sortable();

            $grid->column('created_at');
            $grid->column('refund_id','退款编号');
            $grid->column('es.patient_name','患者姓名');
            $grid->column('visit_id','就诊卡号');
            $grid->column('source.source_name','来源');
            $grid->column('type.bussiness_name','业务类型');
            $grid->column('met.method_name','退款方式');
            $grid->column('money');
            $grid->column('status')->using(['1'=>'<font style="color: green">已退款</font>','2'=>'<font style="color:red;">未退款</font>']);
            $grid->column('balance');
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableRefreshButton();
            $grid->disableCreateButton();
            $grid->disableFilterButton();
            $grid->export()->titles('退款记录')->filename('退款记录')->rows(function ($rows){
                foreach ($rows as $index => &$row){
                    if ($row['status']==1){
                        $row['status']='已退款';
                    }elseif ($row['status']==2){
                        $row['status']='未退款';
                    }
                }
                return $rows;
            });
            // 禁用过滤器按钮
            $grid->disableFilterButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand();
                $filter->panel();
                $filter->between('created_at', '选择日期')->datetime()->width(200);
                $filter->equal('es.patient_name','患者姓名')->width(3);
                $filter->equal('visit_id','就诊卡号')->width(3);
            });

           // $grid->column('updated_at')->sortable();

           // $grid->filter(function (Grid\Filter $filter) {
               // $filter->equal('id');

           // });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        Admin::css('/css/card.css');
        $data = \App\Models\FourRefund::with(['es','source','type','pay','des','met'])->find($id);
        return admin_view('refund',compact('data'));
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new FourRefund(), function (Form $form) {
           // $form->display('id');
            $form->display('created_at');
            $form->text('refund_id');
            $form->select('patient_id')->options(FourPatient::pluck('patient_name','id')->toArray());
            $form->select('visit_id');
            $form->select('source_id')->options(FourSource::pluck('source_name','id')->toArray());
            $form->select('bussiness_type')->options(Fourbusiness::pluck('bussiness_name','id')->toArray());
            $form->select('method_id')->options(FourMethod::pluck('method_name','id')->toArray());
            $form->text('money');
            $form->radio('status')->options(['1'=>'已退款','2'=>'未退款'])->default(0);
            $form->text('balance');
           // $form->display('updated_at');
        });
    }
}
